<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function checkLogin($username,$password){

    $query="SELECT * FROM users WHERE username='".$username."' AND password='".$password."'";

    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);

    if ($num_rows > 0) {
        return false;
    }else {
        return true;
    }
}

function showText($key){
    global $lang,$languageId;
    echo $lang[$key][$languageId];
}

function getCities(){
    $query="SELECT * FROM city";
    $result = mysql_query($query);
    $i=0;
    while($row = mysql_fetch_array($result)){
        $city[$i]['id']=$row['id'];
        $city[$i]['name']=$row['name'];
        $city[$i]['lat']=$row['lat'];
        $city[$i]['lng']=$row['lng'];
        $i++;
    }
    return $city;
}

function getCity($id){
    $query="SELECT * FROM city WHERE id='".$id."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $city['id']=$row['id'];
        $city['name']=$row['name'];
        $city['lat']=$row['lat'];
        $city['lng']=$row['lng'];
    }
    return $city;
}

function getOfferTypes(){
    $i=1;
    $query="SELECT * FROM offer_type";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $offerType[$i]['id']=$row['id'];
        $offerType[$i]['name']=$row['name'];
        $i++;
    }
    return $offerType;
}

function getOfferTypeName($id){
    $query="SELECT * FROM offer_type WHERE id='".$id."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $offerType['id']=$row['id'];
        $offerType['name']=$row['name'];
    }
    return $offerType['name'];
}

function getPropertyTypes(){
    $i=1;
    $query="SELECT * FROM property_type";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $propertyType[$i]['id']=$row['id'];
        $propertyType[$i]['name']=$row['name'];
        $i++;
    }
    return $propertyType;
}

function getPropertTypeName($id){
    $query="SELECT * FROM property_type WHERE id='".$id."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $propertyType['id']=$row['id'];
        $propertyType['name']=$row['name'];
    }
    return $propertyType['name'];
}

function getPropertiesByCity($city){
    $i=0;
    $query="SELECT * FROM property WHERE city='".$city."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property[$i]['id']=$row['id'];
        $property[$i]['area']=$row['area'];
        $property[$i]['price']=$row['price'];
        $property[$i]['details']=$row['details'];
        $property[$i]['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property[$i]['city']=$city['name'];
        
        $property[$i]['lat']=$row['lat'];
        $property[$i]['lng']=$row['lng'];

        $i++;
    }
    return $property;
}

function getAllProperties(){
    $i=0;
    $query="SELECT * FROM property";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property[$i]['id']=$row['id'];
        $property[$i]['type']=getPropertTypeName($row['type']);
        $property[$i]['offertype']=getOfferTypeName($row['offer_type']);
        
        $property[$i]['area']=$row['area'];
        $property[$i]['price']=$row['price'];
        $property[$i]['details']=$row['details'];
        $property[$i]['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property[$i]['city']=$city['name'];
        
        $property[$i]['lat']=$row['lat'];
        $property[$i]['lng']=$row['lng'];

        $i++;
    }
    return $property;
}

function getLastProperties(){
    $i=0;
    $query="SELECT * FROM property ORDER BY id DESC LIMIT 0,30";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property[$i]['id']=$row['id'];
        $property[$i]['type']=getPropertTypeName($row['type']);
        $property[$i]['offertype']=getOfferTypeName($row['offer_type']);
        
        $property[$i]['area']=$row['area'];
        $property[$i]['price']=$row['price'];
        $property[$i]['details']=$row['details'];
        $property[$i]['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property[$i]['city']=$city['name'];
        
        $property[$i]['lat']=$row['lat'];
        $property[$i]['lng']=$row['lng'];

        $i++;
    }
    return $property;
}

function getProperties($city,$offerType,$propertyType,$priceFrom,$priceTo){
    $i=0;
    $query="SELECT * FROM property WHERE (price >='".$priceFrom."' AND price <='".$priceTo."')";
    $query.=" AND cityId='".$city."'";
    $query.=" AND type='".$propertyType."' AND offer_type='".$offerType."'";

    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property[$i]['id']=$row['id'];
        $property[$i]['type']=getPropertTypeName($row['type']);
        $property[$i]['offertype']=getOfferTypeName($row['offer_type']);
        
        $property[$i]['area']=$row['area'];
        $property[$i]['price']=$row['price'];
        $property[$i]['details']=$row['details'];
        $property[$i]['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property[$i]['city']=$city['name'];
        
        $property[$i]['lat']=$row['lat'];
        $property[$i]['lng']=$row['lng'];

        $i++;
    }
    return $property;
}

function getPropertiesNum(){
    $query="SELECT * FROM property";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    return $num_rows;
}

function getApartment($id){
    $i=0;
    $query="SELECT property.*,apartment.story,apartment.totalStory,apartment.livingRoom,apartment.bedRoom,apartment.bath FROM property,apartment WHERE property.id='".$id."'";

    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property['id']=$row['id'];
        $property['type']=getPropertTypeName($row['type']);
        $property['offertype']=getOfferTypeName($row['offer_type']);
        
        $property['area']=$row['area'];
        $property['price']=$row['price'];
        $property['details']=$row['details'];
        $property['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property['city']=$city['name'];
        
        $property['lat']=$row['lat'];
        $property['lng']=$row['lng'];

        //APARTMENT TABLE
        $property['livingRoom']=$row['livingRoom'];
        $property['bedRoom']=$row['bedRoom'];
        $property['bath']=$row['bath'];
        $property['story']=$row['story'];
        $property['totalStory']=$row['totalStory'];
        
        $i++;
    }
    return $property;
}


function getApartments($city,$offerType,$propertyType,$priceFrom,$priceTo){
    $i=0;
    $query="SELECT property.*,apartment.story,apartment.totalStory,apartment.livingRoom,apartment.bedRoom,apartment.bath FROM property,apartment WHERE (price >='".$priceFrom."' AND price <='".$priceTo."')";
    $query.=" AND cityId='".$city."'";
    $query.=" AND type='".$propertyType."' AND offer_type='".$offerType."'";
    $query.=" ORDER BY price ASC";

    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
        $property[$i]['id']=$row['id'];
        $property[$i]['type']=getPropertTypeName($row['type']);
        $property[$i]['offertype']=getOfferTypeName($row['offer_type']);
        
        $property[$i]['area']=$row['area'];
        $property[$i]['price']=$row['price'];
        $property[$i]['details']=$row['details'];
        $property[$i]['registerdate']=$row['registerdate'];
                
        $city=getCity($row['cityId']);
        $property[$i]['city']=$city['name'];
        
        $property[$i]['lat']=$row['lat'];
        $property[$i]['lng']=$row['lng'];
        
        $property[$i]['livingRoom']=$row['livingRoom'];

        $i++;
    }
    return $property;
}

function getPoint($id){
    $pointQuery="SELECT * FROM point WHERE id='".$id."'";
    $pointResult = mysql_query($pointQuery);
    while($row = mysql_fetch_array($pointResult)){
        $point['id']=$id;
        $point['lat']=$row['lat'];
        $point['lng']=$row['lng'];
    }

    return $point;
}

function addProperty($type,$offerType,$date,$area,$price,$details,$city,$lat,$lng){
    $query="INSERT INTO property VALUES('',";
    $query=$query."'".$type."','".$offerType."','".$date."','".$area."','".$price."','".$details."','".$city."','".$lat."','".$lng."')";
    if($result = mysql_query($query)){
        $selQuery="SELECT * FROM property WHERE lat='".$lat."' AND lng='".$lng."'";
        $result2 = mysql_query($selQuery);
        while($row = mysql_fetch_array($result2)){
            $id=$row['id'];
        }
        return $id;
    }else{
        return false;
    }
    
}

function addApartment($propId,$story,$totalStory,$room,$bedRoom,$bath){
    $query="INSERT INTO apartment VALUES('',";
    $query=$query."'".$propId."','".$story."','".$totalStory."','".$room."','".$bedRoom."','".$bath."'";
    if($result = mysql_query($query)){return true;}else{return false;}
}

?>
